import java.util.Arrays;

public class Summarize {
//    习题:
////    有从 1 到 9, 九个数
////    分别 不重复 填到九宫格里. 形如:
////            +---+---+---+
////            | 8 | 3 | 4 |
////            +---+---+---+
////            | 1 | 5 | 9 |
////            +---+---+---+
////            | 6 | 7 | 2 |
////            +---+---+---+
////    要求:
////    每一行的和(如: 4+9+2, 3+5+7), 相等.
////    每一列的和(如: 4+3+8), 相等.
////    各对角线的和(如: 4+5+6), 相等.
    private int[] sq = {1, 2, 3, 4, 5, 6, 7, 8, 9};
//    private int[] sq = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
    private void print() {
        System.out.println("+---+---+---+");
        System.out.println("| " + this.sq[0] + " | " + this.sq[1] + " | " + this.sq[2] + " |");
        System.out.println("+---+---+---+");
        System.out.println("| " + this.sq[3] + " | " + this.sq[4] + " | " + this.sq[5] + " |");
        System.out.println("+---+---+---+");
        System.out.println("| " + this.sq[6] + " | " + this.sq[7] + " | " + this.sq[8] + " |");
        System.out.println("+---+---+---+");
    }
    private static boolean isMaxArr(int[] arr) {
        for(int i = 0; i < arr.length - 1; i++) {
            for(int j = i + 1; j < arr.length; j++) {
                if(arr[i] < arr[j]){
                    return false;
                }
            }
        }
        return true;
    }
    private boolean isLineEqual15(int a, int b, int c) {
        if(a + b + c == 15) {
            return true;
        }
        return false;
    }
    private void swapValByIdx(int idx1, int idx2) {
        int tmp = this.sq[idx1];
        this.sq[idx1] = this.sq[idx2];
        this.sq[idx2] = tmp;
    }
    private int findChangeIdx() {
        for(int i = this.sq.length - 1; i > 0; i--) {
            if(this.sq[i] > this.sq[i - 1]) {
                return i - 1;
            }
        }
        return -1;
    }
    private int findNextIdxByIdx(int fromIdx) {
        int fromNum = this.sq[fromIdx];     // 基值
        int miniNum = 10;                   // 最小值
        int idx = 8;
        for(int i = fromIdx; i < this.sq.length; i++) {
            if(this.sq[i] > fromNum) {
                if(this.sq[i] < miniNum) {
                    miniNum = this.sq[i];
                    idx = i;
                }
            }
        }
        return idx;
    }
    private void reSortFromIdx(int idx) {
        if(idx >= this.sq.length - 2) {
            return ;
        }
        Arrays.sort(this.sq, idx + 1, this.sq.length);
    }
    private void next() {
        if(isMaxArr(this.sq)) {
            return;
        }else {
            int idx = findChangeIdx();
            int nextNumIdx = findNextIdxByIdx(idx);
            this.swapValByIdx(idx, nextNumIdx);
            this.reSortFromIdx(idx);
        }
    }
    private void sort() {
        while(!isMaxArr(this.sq)) {
            boolean r1 = this.isLineEqual15(this.sq[0], this.sq[1], this.sq[2]);
            boolean r2 = this.isLineEqual15(this.sq[3], this.sq[4], this.sq[5]);
            boolean r3 = this.isLineEqual15(this.sq[6], this.sq[7], this.sq[8]);
            boolean c1 = this.isLineEqual15(this.sq[0], this.sq[3], this.sq[6]);
            boolean c2 = this.isLineEqual15(this.sq[1], this.sq[4], this.sq[7]);
            boolean c3 = this.isLineEqual15(this.sq[2], this.sq[5], this.sq[8]);
            boolean q1 = this.isLineEqual15(this.sq[0], this.sq[4], this.sq[8]);
            boolean q2 = this.isLineEqual15(this.sq[2], this.sq[4], this.sq[6]);
            if(r1 && r2 && r3 && c1 && c2 && c3 && q1 && q2) {
                this.print();
                System.out.println("");
            }
            this.next();
        }
    }
    public static void main(String[] args) {
        Summarize sm = new Summarize();
        sm.sort();
    }

}
